(function (vc) {
    vc.extends({
        data: {
            loginInfo: {
                logo: '',
                username: '',
                passwd: '',
                validateCode: '',
                errorInfo: '',
				hasSave: true,
                mobile:'',
                messageCode:''
            },
            showTab:'1',
            yzm_text:'获取验证码',
            disabled_yzm:false,
            show_djs:true,
            captcha_text:60,
            timer:'',
            localUserName:'',
            localUserMobile:''
        },
        _initMethod: function () {
			this.localUserName = window.localStorage.getItem("userName");
            this.localUserMobile = window.localStorage.getItem("userMobile");
            vc.component.clearCacheData();
            vc.component._loadSysInfo();
			if(this.localUserName){
				window.localStorage.setItem("userName",this.localUserName)
			}
            if( this.localUserMobile){
				window.localStorage.setItem("userMobile", this.localUserMobile)
			}
        },
        _initEvent: function () {
            this.localUserName = window.localStorage.getItem("userName");
            this.localUserMobile = window.localStorage.getItem("userMobile");
            // console.log( 56,this.localUserName, this.localUserMobile,this.$that.showTab)
            if( this.localUserMobile){
                this.$that.loginInfo.mobile =  this.localUserMobile;
                    this.$that.loginInfo.username = this.localUserName;
            }
            if(this.localUserName){
                this.$that.loginInfo.username = this.localUserName;
            }
            vc.component.$on('errorInfoEvent', function (_errorInfo) {
                vc.component.loginInfo.errorInfo = _errorInfo;
            });

            vc.component.$on('validate_code_component_param_change_event', function (params) {
                for (var tmpAttr in params) {
                    vc.component.loginInfo[tmpAttr] = params[tmpAttr];
                }
            });
            vc.on('login', 'doLogin', function () {
                vc.component.doLogin();
            })
        },
        methods: {
            clearCacheData: function () {
                vc.clearCacheData();
            },
            _loadSysInfo: function () {
                console.log(45, this.localUserName, this.localUserMobile,   this.loginInfo)
                var param = {
                    params: {
                        sys: 'CH'
                    }
                }
                vc.http.get(
                    'login',
                    'getSysInfo',
                    param,
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        if (res.status != 200) {
                            console.log("加载系统信息失败");
                            vc.saveData("_sysInfo", { logo: 'HC' });
                            vc.copyObject(json, vc.component.loginInfo);
                            return;
                        }
                        vc.copyObject(JSON.parse(json), vc.component.loginInfo);
                        //保存到浏览器
                        vc.saveData("_sysInfo", JSON.parse(json));
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.saveData("_sysInfo", { logo: 'HC' });
                        vc.copyObject(json, vc.component.loginInfo);
                        vc.component.loginInfo.errorInfo = errInfo;
                    });
            },
            doLogin: function () {
                var params='';
                if(this.showTab=='1'){
                    if (!vc.notNull(vc.component.loginInfo.mobile)) {
                        vc.toast('手机号不能为空');
                        return;
                    }
                    if (!vc.notNull(vc.component.loginInfo.messageCode)) {
                        vc.toast('短信验证码不能为空');
                        return;
                    }
                    params={
                        mobile:this.loginInfo.mobile,
                        channelNo:'blogpcsms',
                        scene:'pcSmsLogin',
                        messageCode:this.loginInfo.messageCode,
                        hasSave:this.loginInfo.hasSave,
                    }
                }else{
                    if (!vc.notNull(vc.component.loginInfo.username)) {
                        vc.toast('用户名不能为空');
                        return;
                    }
                    if (!vc.notNull(vc.component.loginInfo.passwd)) {
                        vc.toast('密码不能为空');
                        return;
                    }
                    if (!vc.notNull(vc.component.loginInfo.validateCode)) {
                        vc.toast('验证码不能为空');
                        return;
                    }
                    params={
                        logo:this.loginInfo.logo,
                        account:this.loginInfo.username,
                        passwd:this.loginInfo.passwd,
                        validateCode:this.loginInfo.validateCode,
                        channelNo:'blogpcaccount',
                        errorInfo:this.loginInfo.errorInfo,
                        hasSave:this.loginInfo.hasSave,
                    }
                }
                // console.log(this.loginInfo)
                vc.http.post(
                    'login',
                    'doLoginPc?version=2.0',
                    JSON.stringify(params),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _data = JSON.parse(json);
                        if (_data.hasOwnProperty('code') && _data.code != '0') {
                            vc.toast(_data.msg);
                            return;
                        }
                        if(vc.component.loginInfo.hasSave){
                            if(vc.component.showTab=='1'){
                                window.localStorage.setItem("userMobile",vc.component.loginInfo.mobile)
                            }else{
                                window.localStorage.setItem("userName",vc.component.loginInfo.username)
                            }
                        }else {
                            if(vc.component.showTab=='1'){
                                window.localStorage.setItem("userMobile",'')
                            }else{
                                window.localStorage.setItem("userName",'')
                            }
                        }

                        if (res.status == 200) {
                            vc.jumpToPage('/');
                            return;
                        }
                        vc.toast(json);
                        vc.component.loginInfo.errorInfo = json;
                    },
                function (errInfo, error) {
                    console.log('请求失败处理');
                    vc.toast(errInfo);
                    vc.component.loginInfo.errorInfo = errInfo;
                });


            },
            _doRegister: function () {
                vc.jumpToPage('/user.html#/pages/frame/register');
            },
            //短信-密码登录切换
            clickTabActive(type){
                this.showTab=type;
                if(type=='1'){
                    // this.loginInfo.mobile=this.localUserMobile;
                    // this.loginInfo.messageCode='';
                }else{
                    // this.loginInfo.username=this.localUserName;
                    // this.loginInfo.passwd='';
                    // this.loginInfo.validateCode='';
                }
            },
            //获取手机验证码
            sendyzm(){
                if(this.showTab=='1'){//短信登录
                    if(this.loginInfo.mobile==''){
                        vc.toast('手机号不能为空');
                        return false;
                    }
                    if(!(/^1[3456789]\d{9}$/.test(this.loginInfo.mobile))){
                        vc.toast('手机号格式错误');
                        return false;
                    }
                    this.getCaptcha();//发送获取短信请求
                }
            },
            //获取短信验证码
            getCaptcha(){
                var that=this;
                var host = window.location.protocol+'//'+window.location.host;
                var smsValidateCode = host+'/app/smsValidateCode';
                var requestHeaders=getRequestHeaders('01',communityId);//获取请求头
                let params = {
                    mobile: that.loginInfo.mobile,
                    smsType:'3',
                    scene:'pcSmsLogin'
                }
                // that.disabled_yzm=true;
                // that.yzm_text='重新发送'
                console.log(requestHeaders)
                $.ajax({
                    url : smsValidateCode,
                    type: 'POST',
                    contentType: 'text/plain',
                    headers: requestHeaders,
                    data: JSON.stringify(params),
                    success : function (res){
                        var data = JSON.parse(res);
                        if(data.code=='200'){
                            vc.toast('短信发送成功');
                            that.disabled_yzm=false;
                            that.getCode();
                        }else{
                            vc.toast(data.msg);
                            that.disabled_yzm=false;
                            that.yzm_text='发送验证码'
                        }
                    },
                    error:function(res){
                        vc.toast('请求超时');
                        that.disabled_yzm=false;
                        that.show_djs=true;
                        that.captcha_text=60;
                        that.yzm_text='发送验证码';
                        clearInterval(that.timer);
                    }
                })
            },
            //倒计时
            getCode() {
                var that=this;
                that.show_djs = false;
                that.captcha_text=60;
                that.timer = setInterval(()=>{
                    that.captcha_text--
                    if(that.captcha_text===0){
                        that.show_djs = true
                        clearInterval(that.timer);
                    }
                },1000)
            },
        },
        _destroyedMethod: function () {
            console.log("登录页面销毁调用");
        }
    });


})(window.vc);